package com.atguigu.spzx.manger.controller;

import com.atguigu.spzx.manger.service.SysUserService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Operation(summary = "登入方法")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取验证码")
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo = sysUserService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取userInfo")
    @GetMapping("/userinfo")
    public Result getUserInfo(@RequestHeader(name = "token") String token){
        SysUser user = sysUserService.getUserInfo(token);
        return Result.build(user, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "退出登入")
    @GetMapping("/logout")
    public Result logout(@RequestHeader(name = "token") String token){
        sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    public static void main(String[] args) {
//        String lastDayOfMonth = getLastDayOfMonth("2024-03");
//        System.out.println(lastDayOfMonth);
        Set<Integer> set1 = new HashSet<>();
        set1.add(1);
        set1.add(2);
        set1.add(3);


        Set<Integer> set2 = new HashSet<>();
        set2.add(3);
        set2.add(4);
        set2.add(5);

        boolean hasIntersection = hasIntersection(set1, set2);
        System.out.println("Has intersection: " + hasIntersection);
    }

    public static String getLastDayOfMonth(String yearMonth) {
        // 解析年份和月份
        String[] parts = yearMonth.split("-");
        int year = Integer.parseInt(parts[0]);
        int month = Integer.parseInt(parts[1]);

        // 获取该月份的最后一天
        LocalDate lastDay = LocalDate.of(year, month, 1).plusMonths(1).minusDays(1);
        System.out.println(lastDay);
        return lastDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static boolean hasIntersection(Set<Integer> set1, Set<Integer> set2) {
        // 创建一个临时集合存储 set1 的元素
        Set<Integer> tempSet = new HashSet<>(set1);

        // 保留 set1 中与 set2 中相同的元素
        tempSet.retainAll(set2);

        System.out.println(tempSet);
        // 如果临时集合不为空，则说明存在交集，返回 true；否则返回 false
        return !tempSet.isEmpty();
    }

}
